package main

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"log"
	"net"
	"time"
)

type frame struct {
	packageLength int32
	headerLength  int16
	version       int16
	operation     int16
	seqid         int32
	datad         string
}

func MegGen() []byte {

	datad := "abacdsfessfdsf"
	f := frame{}
	f.packageLength = int32(len(datad)) + 16
	f.headerLength = 16
	f.version = 0
	f.operation = 0
	f.seqid = 1
	f.datad = datad
	fmt.Println(f)
	b := make([]byte, 0)
	bpl := make([]byte, 4)
	binary.BigEndian.PutUint32(bpl, uint32(f.packageLength))
	bhl := make([]byte, 2)
	binary.BigEndian.PutUint16(bhl, uint16(f.headerLength))
	bvs := make([]byte, 2)
	binary.BigEndian.PutUint16(bvs, uint16(f.version))
	bop := make([]byte, 4)
	binary.BigEndian.PutUint32(bop, uint32(f.operation))
	bsq := make([]byte, 4)
	binary.BigEndian.PutUint32(bsq, uint32(f.seqid))
	b = append(b, bpl...)
	b = append(b, bhl...)
	b = append(b, bvs...)
	b = append(b, bop...)
	b = append(b, bsq...)
	b = append(b, []byte(f.datad)...)
	return b

}

func main() {
	data := MegGen()
	buf := bytes.NewBuffer(data)
	buf2 := bytes.NewBuffer(data)
	conn, err := net.DialTimeout("tcp", "localhost:8888", time.Second*30)
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	_, err = conn.Write(buf.Bytes())
	_, err = conn.Write(buf2.Bytes())
	if err != nil {
		log.Fatal(err)
	}

}
